﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Configuration;

namespace ProductManager.gui
{
    public partial class GUIConfig : DevExpress.XtraEditors.XtraForm
    {
        bool check = false;
        public GUIConfig()
        {
            InitializeComponent();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            string s = string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}",txtServerName.Text,txtDatabaseName.Text,txtUserID.Text,txtPassword.Text);
            SqlConnection conn = new SqlConnection(s);
            try
            {
                conn.Open();
                check = true;
            }
            catch (Exception ex)
            {
                check = false;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }

            if (check)
            {
                MessageBox.Show(this, "Connect successfull!", "Connection", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else {
                MessageBox.Show(this, "Connect error!", "Connection", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Dispose();   
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string s = string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}", txtServerName.Text, txtDatabaseName.Text, txtUserID.Text, txtPassword.Text);
            SqlConnection conn = new SqlConnection(s);
            try
            {
                conn.Open();
                check = true;
            }
            catch (Exception ex)
            {
                check = false;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }

            if (check)
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                config.ConnectionStrings.ConnectionStrings["ProductManager.Properties.Settings.ProductManagersConnectionString"].ConnectionString = s;
                config.Save(ConfigurationSaveMode.Modified, true);
                ConfigurationManager.RefreshSection("connectionStrings");
                MessageBox.Show(this, "Save successfull!", "Update connection", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(this, "Connect error!", "Update connection", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}